package com.bawei.fuxi.ui.home;

import com.bawei.fuxi.base.mvp.BasePresenter;
import com.bawei.fuxi.entity.BannerEntity;
import com.bawei.fuxi.entity.VideoEntity;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class HomePresenter extends BasePresenter<HomeContract.IHomeView> implements HomeContract.IHomePresenter {
    private HomeContract.IHomeModel mIHomeModel;
    public HomePresenter() {
        mIHomeModel = new HomeModel();
    }

    @Override
    public void video(int currentPage, int pageSize) {
        mIHomeModel.video(currentPage, pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<VideoEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(VideoEntity videoEntity) {
                        if(isAttach()){
                            getView().showVideo(videoEntity);
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        if(isAttach()){
                            getView().onError();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void search(String keywords) {
        mIHomeModel.search(keywords)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<VideoEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(VideoEntity videoEntity) {
                        if(isAttach()){
                            getView().showSearch(videoEntity);
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        if(isAttach()){
                            getView().onError();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void banner() {
        mIHomeModel.banner()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BannerEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BannerEntity bannerEntity) {
                        if(isAttach()){
                            getView().showBanner(bannerEntity);
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        if(isAttach()){
                            getView().onError();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
